/**
 * Zhe Jiang HongCheng System Inc.
 * Copyright All Rights Reserved.
 */
package com.zjhcsoft.jbosscache.util;

import junit.framework.Assert;
import org.jboss.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * FileName : WebContextHolder.java
 * CreateTime : 11-11-11 上午9:07
 * Description : WebContextHolder
 *
 * @author : liuys
 * @version : 1.0
 */
public class WebContextHolder {
    private static Logger logger = LoggerFactory.getLogger(WebContextHolder.class);

    private static WebContextHolder contextHolder = new WebContextHolder();
    private static Cache<String, Object> cache = null;

    private WebContextHolder() {
    }

    public static WebContextHolder getInstance() {
        return WebContextHolder.contextHolder;
    }

    public void setJbossCache(Cache<String, Object> cache) {
        logger.debug("注入Jboss Cache到WebContextHolder.");
        WebContextHolder.cache = cache;
    }

    public Cache<String, Object> getJbossCache() {
        assertJbossCacheInjected();
        return WebContextHolder.cache;
    }

    public static void clearHolder() {
        logger.debug("释放WebContextHolder中的Jboss Cache.");
        WebContextHolder.cache.getRoot().clearData();
        WebContextHolder.cache.stop();
        WebContextHolder.cache = null;
    }

    private static void assertJbossCacheInjected() {
        Assert.assertTrue("Jboss Cache 未注入.", cache != null);
    }
}